package cn.tedu.charging.order.mqtt;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
@Slf4j
@Component
public class MqttProducer {
    @Autowired
    private MqttClient mqttClient;

    public void send(String topic, String message) {
        try {
            mqttClient.publish(topic,message.getBytes(),0,false);
            log.debug("发送消息到EMQX,topic:{},message:{},成功", topic, message);
        } catch (MqttException e) {
            throw new RuntimeException(e);
        }
    }
}
